from django.db import models
from dvadmin.utils.models import CoreModel
from django.conf import settings

class Device(CoreModel):
    """设备管理模型"""
    name = models.CharField(max_length=100, verbose_name="设备名称")
    model = models.CharField(max_length=100, verbose_name="设备型号")
    status_choices = (
        (1, "正常运行"),
        (2, "待维护"),
        (3, "维护中"),
        (4, "已停用")
    )
    status = models.IntegerField(choices=status_choices, default=1, verbose_name="运行状态")
    last_maintenance = models.DateField(null=True, blank=True, verbose_name="上次维护时间")
    next_maintenance = models.DateField(null=True, blank=True, verbose_name="下次维护时间")
    description = models.TextField(null=True, blank=True, verbose_name="设备描述")
    creator = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True, verbose_name="创建人", related_name="device_creator")
    modifier = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True, verbose_name="修改人", related_name="device_modifier")

    class Meta:
        db_table = "device"
        verbose_name = "设备管理"
        verbose_name_plural = verbose_name
        ordering = ('-create_datetime',)

class MaintenanceRecord(CoreModel):
    """设备维护记录"""
    device = models.ForeignKey(Device, on_delete=models.CASCADE, verbose_name="设备")
    maintenance_type_choices = (
        (1, "定期保养"),
        (2, "故障维修"),
        (3, "设备升级")
    )
    maintenance_type = models.IntegerField(choices=maintenance_type_choices, verbose_name="维护类型")
    status_choices = (
        (1, "待审批"),
        (2, "已审批"),
        (3, "进行中"),
        (4, "已完成")
    )
    status = models.IntegerField(choices=status_choices, default=1, verbose_name="状态")
    description = models.TextField(verbose_name="维护内容")
    completion_date = models.DateField(null=True, blank=True, verbose_name="完成时间")
    creator = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True, verbose_name="创建人", related_name="maintenance_creator")
    modifier = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True, verbose_name="修改人", related_name="maintenance_modifier")
    
    class Meta:
        db_table = "maintenance_record"
        verbose_name = "维护记录"
        verbose_name_plural = verbose_name
        ordering = ('-create_datetime',)
